#ifndef THREAD_H
#define THREAD_H

#include <stdint.h>

// 主线程除外
#define MAX_THREAD 4

// 4M stack size
#define STACK_SIZE (1024*1024*4)

// 线程状态
typedef enum {
    TERMINATED, RUNNABLE 
} Status;

typedef struct Frame{
    uint64_t rsp;
    uint64_t rdi;
    uint64_t rsi;
    uint64_t rbp;
    uint64_t rbx;
    uint64_t rdx;
    uint64_t rcx;
    uint64_t rax;
    uint64_t rflags;
    uint64_t rip;

    // 线程结束时的返回地址
    uint64_t exit_rip;
} Frame;

typedef struct Thread{
    // 线程相关的一些信息
    // 一个线程对应一个Thread对象, 存储在数组中
    int thread_id;
    Status status;

    // frame, 用于恢复运行
    Frame frame;

    // 栈空间
    char* stack;
    unsigned stackSize;

    void* ret;

} Thread;

typedef int thread_t;

int thread_create(thread_t* thread, void*(*start)(void*), void* arg);

int thread_join(thread_t thread, void** ret);

int thread_yield();

#endif // THREAD_H

